part of 'table.dart';

class Diary implements Identity {
  @override
  final int id;
  final Date date;
  String title;
  String content;

  Diary({
    required this.id,
    required this.date,
    required this.title,
    required this.content,
  });

  factory Diary.fromRaw(Json raw) {
    dynamic dateData = raw.getField("date");
    late final Date date;
    if (dateData is DateTime) {
      // 这是从Postgresql恢复的情况
      date = Date.fromDateTime(dateData.toLocal());
    } else if (dateData is String) {
      // 这是从传输与json恢复的情况
      date = Date.parse(dateData);
    }

    return Diary(
      date: date,
      id: raw["id"],
      title: raw.getField<String>("title"),
      content: raw.getField<String>("content"),
    );
  }

  @override
  Json toJson() => {
        "id": id,
        "date": date.toString(),
        "title": title,
        "content": content,
      };
}
